package com.wzq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.wzq.dao.pojo.*;
import com.wzq.service.IDurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/duration")
public class DurationController {

    @Autowired
    private IDurationService iDurationService;
    //   添加
    @PostMapping
    public Result save(@RequestBody Duration duration) {
        boolean flag = iDurationService.save(duration);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
    //   修改
    @PutMapping
    public Result update(@RequestBody Duration duration) {
        boolean flag = iDurationService.updateById(duration);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    //    id删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = iDurationService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    //    获得id对应用户信息
    @GetMapping("/{p_id}/app")
    public Result getById(@PathVariable Integer p_id) {
        if (p_id==null)
            return new Result(Code.GET_ERR,null);
        LambdaQueryWrapper<Duration> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Duration::getP_id,p_id);
        Duration duration = iDurationService.getOne(lqw);
        Integer code = duration != null ? Code.GET_OK : Code.GET_ERR;
        String msg = duration != null ? "" : "数据查询失败，请重试！";
        return new Result(code,duration,msg);
    }

    //     获得所有用户信息
    @GetMapping
    public Result getAll() {
        List<Duration> list = iDurationService.list();
        System.out.println(list);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }



}
